/*
 *  FILE: 
 *  	time.c
 *
 *  DESCRIPTION:
 *  	Wrapper around the confusing ANSI C time functions.
 *      These functions are only useful for grabbing the current GMT time ANSI C format,
 *      or for grabbing the last modified date of any local file int GMT ANSI C format.
 *
 *  FUNCTIONS:
 *  	gmt_asctime_r()      - Reentrant style function for grabbing ANSI C time in GMT 
 *  	file_modified_date() - Get Last Modified ANSI C time in GMT for a specific file.
 *
 *
 * Copyright (c) 2004-2006 Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *         This product includes software developed by
           Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * 4. Neither Karlan Thomas Mitchell nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY KARLAN THOMAS MITCHELL AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "time.h"

#ifdef WIN32
void sleep(int seconds){
    DWORD milliseconds = seconds * 1000;
    _sleep(milliseconds);
    }
void usleep(int microseconds){
    DWORD milliseconds = microseconds / 1000;
    _sleep(milliseconds);
    }
#endif    
    
char * gmt_asctime_r (char * result){
	time_t time_t_data;
	struct tm  st_tm;
	time(&time_t_data);
    #ifdef __WIN32__
	/*Windows doesn't contain the re-entrant version....it must be thread safe...*/
    	st_tm = *gmtime(&time_t_data);
	strcpy(result, asctime(&st_tm));
    #else
    	gmtime_r(&time_t_data, &st_tm);
	#ifdef SUNOS
    	asctime_r(&st_tm, result, 26);
	#else
    	asctime_r(&st_tm, result);
	#endif
    #endif
        return (char *)result;
	}


char * file_modified_date(char * filename, char * date_buffer){
	/*The two structs below are for file time*/
	struct stat   file_stat; 
	struct tm     file_tm;
	
	stat(filename, &file_stat);
    #ifdef __WIN32__
    	file_tm = *gmtime(&file_stat.st_mtime);
        strcpy(date_buffer, asctime(&file_tm));
    #else
    	gmtime_r(&file_stat.st_mtime, &file_tm);
	#ifdef SUNOS
    	asctime_r(&file_tm, date_buffer, 26);
	#else
    	asctime_r(&file_tm, date_buffer);
	#endif
    #endif
	return (char *)date_buffer;
} 
